import requests
from bs4 import BeautifulSoup


# 发送HTTP请求
url = "https://top.baidu.com/board?tab=realtime"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
response = requests.get(url, headers=headers)

# 检查请求是否成功
if response.status_code == 200:
    # 解析HTML内容
    soup = BeautifulSoup(response.text, 'html.parser')

    items = soup.select("div.category-wrap_iQLoo.horizontal_1eKyQ")
    for item in items:
        title = item.select_one('div.c-single-text-ellipsis')
        link = item.select_one('a.img-wrapper_29V76')
        print(title.text, link['href'])

    # # 提取热搜标题
    # hot_titles = soup.select("div.c-single-text-ellipsis")
    #
    # # 提取热搜的超链接
    # link_url = soup.select("a.item-wrap_2oCLZ")
    #
    # data = list(zip(hot_titles, link_url))
    #
    # for i in data:
    #     print(i[0].text, i[1.13模块编程.12package]['href'])
else:
    print(f"请求失败，状态码: {response.status_code}")
